package com.wxcms.ctrl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wxapi.process.MpAccount;
import com.wxapi.process.WxApiClient;
import com.wxapi.process.WxMemoryCacheClient;
import com.wxcms.domain.AccountFans;
import com.wxcms.domain.AccountOrg;
import com.wxcms.domain.Org;
import com.wxcms.service.AccountFansService;
import com.wxcms.service.AccountOrgService;
import com.wxcms.service.OrgService;

/**
 * 
 */

@Controller
@RequestMapping("/wxcms/accountorg")
public class AccountOrgCtrl{

	@Autowired
	private AccountOrgService entityService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private AccountFansService fansService;
	
	@RequestMapping(value = "/getById")
	public ModelAndView getById(String id){
		entityService.getById(id);
		return new ModelAndView();
	}

	@RequestMapping(value = "/list")
	public  ModelAndView list(@ModelAttribute AccountOrg searchEntity){
		ModelAndView modelAndView = new ModelAndView("wxcms/accountOrgList");
		List<AccountOrg> list = entityService.listForPage(searchEntity);
		modelAndView.addObject("pageList", list);
		modelAndView.addObject("cur_nav", "accountorg");
		return modelAndView;
	}
	
	@RequestMapping(value = "/toMerge")
	public ModelAndView toMerge(AccountOrg entity){
		ModelAndView modelAndView = new ModelAndView("wxcms/accountOrgMerge");
		AccountOrg accountOrg = new AccountOrg();
		if(entity.getId()!=null){
			accountOrg = entityService.getById(entity.getId().toString());
		}
		else {
			AccountFans fans = fansService.getByOpenId(entity.getOpenid());
			accountOrg.setNicknameStr(fans.getNicknameStr());
			accountOrg.setOpenid(fans.getOpenId());
		}
		List<Org> orgList = orgService.listForPage(null);
		modelAndView.addObject("entity", accountOrg);
		modelAndView.addObject("orgList", orgList);
		return modelAndView;
	}

	@RequestMapping(value = "/doMerge")
	public ModelAndView doMerge(AccountOrg entity){
		if(entity.getId()!=null){
			entityService.update(entity);
		}
		else {
			entityService.add(entity);
		}
		return new ModelAndView("redirect:/wxcms/accountorg/list.html");
	}

	@RequestMapping(value = "/delete")
	public ModelAndView delete(AccountOrg entity){
		ModelAndView modelAndView = new ModelAndView("redirect:/wxcms/accountorg/list.html");
		entityService.delete(entity);
		return modelAndView;
	}
	
}
